package com.wft.chapter15;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class RandomFileChannelTest {
    public static void main(String[] args) {
        File file = new File("D:\\Workspaces2018\\crazy-java\\src\\main\\java\\com\\wft\\chapter15\\FileChannelTest.java");
        try (
                RandomAccessFile raf = new RandomAccessFile(file, "rw");
                FileChannel fileChannel = raf.getChannel();
        ) {
            MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, file.length());
            // 将Channel的position指向文件最后
            fileChannel.position(file.length());
            // 将buffer中的内容输入到文件中
            fileChannel.write(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
